
#include "htmlgenerator.h"

HtmlGenerator::~HtmlGenerator()
{
    /////db->Purge();
	  //////////delete db;
}

HtmlGenerator::HtmlGenerator( DB_Manager * dbin  , int db_id  , int fontsize )
{
  db = dbin;
	cleaner = new QTidy();  
	cleaner->Init(QDir::homePath()+"/.qtity/");
	isConnect = true;
	///////////isConnect = db->Connect(conntrac,db_id);   /* default is 1 */
	enable_doc = false;
	id = db_id;
	qDebug() << "### transall  " << transall.size();
	qDebug() << "### start id  " << id;
	nummer.setNum(db_id);
  POINT.setNum(fontsize);
	Tipo_Oggetto = 1;
	languages = config_language_short();
  xp0 = "<p style=\"margin-top:0px; margin-bottom:0px; margin-left:0px; margin-right:0px; -qt-block-indent:0; text-indent:0px; font-size:"+POINT+"pt;\">";
	xpx = "<p align=\"center\" style=\"margin-top:0px; margin-bottom:0px; margin-left:0px; margin-right:0px; -qt-block-indent:0; text-indent:0px; font-size:"+POINT+"pt;\">";
	xpR = "<p align=\"right\" style=\"margin-top:0px; margin-bottom:0px; margin-left:0px; margin-right:0px; -qt-block-indent:0; text-indent:0px; font-size:"+POINT+"pt;\">";
	xp1 = "</p>\n";
}

void HtmlGenerator::init()
{

QTimer::singleShot(10, this, SLOT(realinit()));  


}



void HtmlGenerator::realinit()
{
	/////////qDebug() << "### isConnect  " << isConnect;
	
	if (!isConnect) {
		emit document_having(false,id);
	}
	
	  transall = db->Translated_Doc();
	qDebug() << "### transall  " << transall.size();
	  /* tutte le icone presenti */
  datiicone = db->GetIcons();
  if (datiicone.size() > 0) {
  TabIcone DDtable = OpenTabelicon(datiicone);
  icones = DDtable.all_lines;
  }
	
	Dpdf.clear();
  wwwpage.clear();
  Fimg.clear();
  Fdoc.clear();
  qtrans.clear();
	
	daticalendario = db->Get_Calendar(id);
	QDate now = QDate::currentDate();
	anno_adess0 = now.year();
	int MaxY = anno_adess0 + 1;
  int MinY = anno_adess0 - 1;
  QCaliVuoto.InitRange(MinY,MaxY,id); 
	if (daticalendario.size() > 0) {
	TableCalendar ExternCalendar = openCalendar(daticalendario);
	QCaliVuoto.MemoDay = ExternCalendar.Works();
	}
	qDebug() << "### calendar -> " << QCaliVuoto.MemoDay.size();
	
	qbase.clear();
	FattorePerFranco = setter.value("valuta/dayCHF").toDouble();
	QString dataeuupdate = setter.value("valuta/Update").toDate().toString("yyyy-MM-dd");
  
	QStringList feldnames = db->FieldList(TABELLA_CASE);
	QStringList itemselect;
	QStringList mappers;
	for (int i = 0; i < feldnames.size(); ++i)  {
		    QString n = feldnames.at(i);
				itemselect.append(n + " AS " "_"+n+"_");
		    mappers.append("_"+n+"_");
				qbase.insert("_"+n+"_",QString());
	}
	QString xselect = "SELECT " +  itemselect.join(",") + " FROM " + QString(TABELLA_CASE) + " WHERE ID="+nummer+" LIMIT 1"; 
	QSqlQueryModel *mod = new QSqlQueryModel();
	mod->setQuery(xselect,db->current());
	const int summline = mod->rowCount();
	
	if ( summline > 0) {
	
	 for (int e = 0; e < summline; ++e) {
								  QSqlRecord r = mod->record(e);  /* records */
	                            for (int i = 0; i < mod->columnCount(); ++i) {
																    const QString Hname = mod->headerData(i,Qt::Horizontal,Qt::DisplayRole).toString(); 
																    QString value = Qt::escape(r.value(Hname).toString());
																     if (Hname == "_TXTIT_101_") {
																			  value = texter(value);
																		 } else if (Hname == "_TXTDE_102_") {
																			  value = texter(value);
																		 } else if (Hname == "_TXTFR_103_") {
																			  value = texter(value);
																		 } else if (Hname == "_TXTEN_104_") {
																			  value = texter(value);
																		 } else if (Hname == "_NOTE_95_") {
																			  value = texter(value);
																		 }  else if (Hname == "_TXT_100_") {
																			  value = texter(value);
																		 }  else if (Hname == "_TIPO_1_") {
																			  Tipo_Oggetto = r.value(Hname).toInt();
																		 }
																		 
																     qbase.insert(Hname,value);
															}
		}
	
	
  ImageUrlFirst = QString("./%1_0.png").arg(nummer);
	TAGIMAGEONE = "<div id=\"PicPosiHouses\"><img id=\"MainHousesPics\" src=\""+ImageUrlFirst+"\" /><div>";

	
  ///////QPixmap imfirst;
  ////////imfirst = createNullPic(IMM_DIMENSIONE_MASSIMA,50);
  ImageListStream = db->Get_Setting(id,"APP_PICS");
	if (ImageListStream.size() > 0) {
  /* foto del oggetto */
	lipic = OpenImageGroup(ImageListStream);
     if (lipic.size() > 0) {
      ///////SPics conni = lipic.first();
      /////imfirst = conni.pix();
     }
	}
  Fdoc.clear();
  Fimg.clear();
  /////////append_pic(ImageUrlFirst,imfirst);
	
	/* load data */
	
	
	
                 /* create document to all language !! */
                 for (int i = 0; i < languages.size(); ++i)   {
                   LoadTrans(i);   /* load current language */
									   const QString name = languages.at(i).toLower();
									 
									  html = QString();
									  out.setString(&html);
									  //////////////////out << "<div class=\"" << languages.at(i) << "\">" << endl;
                    out << TitoloGrande() << endl;
									  out << xp0 << "<img src=\"./dummyspace.png\" />" << xp1 << endl; 
                    out << "<h3>" <<  CPAPaese( qbase.value("_CID_4_"))   <<  "</h3>" << endl; 
                    out << "<br/>" << endl;
                    out << "<table border=\"0\" style=\"margin-top:0px; margin-bottom:0px; margin-left:0px; margin-right:0px;\" align=\"left\" width=\"100%\" cellspacing=\"0\" cellpadding=\"0\" bgcolor=\"#ffffff\">" << endl;
									 	out << "<tr><td width=\"80%\">" << endl;
										/* paese cpa tel fax mail web  */
                    out << addo() << endl;
										out << "<br/><br/>" << endl;
									 
										out << "</td>" << endl;
										out << "<td width=\"20%\">" << endl;
										out << TAGIMAGEONE << endl; 
										out << "</td></tr>" << endl;
										out << "<tr>" << endl;
										out << "<td colspan=\"2\">" << endl;
										out << letti() << endl;
										out << "<br/><br/>" << endl;
                    out << "<p class=\"base\">" << text() << "</p>" << endl;
                    out << "</p><br/>" << endl;
										out << xp0 << "<img src=\"./dummyspace.png\" />" << xp1 << endl; 
										out << "</td>" << endl;
										out << "</tr>" << endl;
										out << "</table>" << endl; 
										
                   //////html += legenda_calendario();
                   
										////////////out << "</div>" << endl;
										out.flush();
									 
                   Fdoc.insert(name,html);
                 }
								 
								 Dpdf.clear();
								 wwwpage.clear();
								 ////////QPixmap orilogo(":/img/vm_color.png");
								 //////QPixmap itlogo = orilogo.scaledToHeight(120,Qt::SmoothTransformation);
								 
										for (int i = 0; i < languages.size(); ++i)   {
									   const QString name = languages.at(i).toLower();
											LoadTrans(i);   /* load current language */
									   QTextDocument *xdoc = new QTextDocument();
										 /////////xdoc->addResource( QTextDocument::ImageResource,QUrl("/base/logo/logo.png"),itlogo);
										 html = QString();
									   out.setString(&html);
										 out << "<table border=\"0\" style=\"margin-top:0px; margin-bottom:0px; margin-left:0px; margin-right:0px;\" align=\"left\" width=\"100%\" cellspacing=\"0\" cellpadding=\"0\" bgcolor=\"#ffffff\">" << endl;
										 out << "<tr><td width=\"8%\">"+xp0+xp1+"</td>" << endl;
										 out << "<td width=\"93%\">" << endl;
										 out << xp0 << "<img src=\"/base/logo/logo.png\" />" << xp1 << endl; 
										 out << xp0 << "<br>" << xp1 << endl; 
										 out << Fdoc.value(name) << endl; 
										 out << "<br/>" << endl;
										 out << caratteristiche(false) << endl;
										 out << "<br>" << endl;
										 out << calendario(false) << endl; 
										 out << xp0 << "<br>" << xp1 << endl;
										 out << xp0 << "<br><br>* Euro update on: " << dataeuupdate << xp1 << endl; 
										 out << "</td>" << endl;
										 out << "</tr>" << endl;
										 out << "</table>" << endl; 
										 out.flush();
										 xdoc->setHtml(html);
										 /////////fillImage( xdoc );
									   Dpdf.insert(name,xdoc);
								    }
										
										doc.clear();
										QDomElement root = doc.createElement("base");
										root.setAttribute ("id",id);
										root.setAttribute ("cmd","replace");
										root.setAttribute ("title",qbase.value("_NOME_2_"));
	                  doc.appendChild(root);
										
										for (int i = 0; i < languages.size(); ++i)   {
									   const QString name = languages.at(i).toLower();
											 LoadTrans(i);   /* load current language */
                       QDomElement page = doc.createElement("page");
											 page.setAttribute ("la",name);
											 root.appendChild(page);
											
											 QTextDocument *Ftxt = Dpdf.value(name);
											 QString ztxt = Ftxt->toPlainText();
											         ztxt = ztxt.replace("\n"," ");
											         ztxt = ztxt.replace("\t"," ");
											         ztxt = ztxt.replace("\r"," ");
											 QDomElement words = doc.createElement("dbwords");
											   QString dbcom = DBWordInsert(Qt::escape(ztxt),name);
											 words.appendChild(doc.createTextNode(dbcom));
											 page.appendChild(words);
											
											 QDomElement bb = doc.createElement("body");
											 bb.setAttribute ("xmlns","http://www.w3.org/1999/xhtml");
											 page.appendChild(bb);
											
										 html = QString();
									   out.setString(&html); 
										 out << "<div class=\"HousePage\">" << endl;
										 out << Fdoc.value(name) << endl; 
										 out << "<br/>" << endl;
										 out << caratteristiche(true) << endl;
										 out << "<br>" << endl;
										 out << calendario(true) << endl;
										 out << "<br>" << endl;
										 out << "<p class=\"endbase\">* Euro update on: " << dataeuupdate << xp1 << endl; 
										 out << "</div>" << endl;
										 out.flush();
										 doc.insertFragment(bb,HandlerWebCode(html));
									   wwwpage.insert(name,page);
								    }
								     ////////////////////qDebug() << "### image size -> " << Fimg.size();
								 
								 enable_doc = true;
		}  /*  if summ line bigger as 0 */
		
    qDebug() << "### last action " << enable_doc;
	
	emit document_having(enable_doc,id);
}

QString HtmlGenerator::DBWordInsert( QString txtdb , const QString la )
{
	QStringList insert;
	            insert.clear();
	            insert.append(QString("'%2-%1'").arg(la).arg(id));
	            insert.append(QString("'%1'").arg(id));
	            insert.append(QString("'%1'").arg(qbase.value("_TIPO_1_")));  /* tipo TIPO_1  */
	            insert.append(QString("'%1'").arg(qbase.value("_CID_4_")));  /* tipo _CID_4_  id paese   */
	            insert.append(QString("'%1'").arg(G_Quote(qbase.value("_NOME_2_"))));
	            insert.append(QString("'%1'").arg( G_Quote(  CPAPaese( qbase.value("_CID_4_") )  ) ));
	            insert.append(QString("'%1'").arg(G_Quote(txtdb)));  /* db words */
	            insert.append(QString("'%1'").arg(la));
	            insert.append(QString("'%1'").arg(G_Quote(qbase.value("_TEL_7_"))));
	            insert.append(QString("'%1'").arg(G_Quote(qbase.value("_TEL_8_"))));
							insert.append(QString("'%1'").arg(G_Quote(qbase.value("_FAX_9_"))));
							insert.append(QString("'%1'").arg(G_Quote(qbase.value("_MAIL_10_"))));
	            insert.append(QString("'%1'").arg(G_Quote(qbase.value("_NATEL_12_"))));
							insert.append(QString("'%1'").arg(G_Quote(qbase.value("_WWW_11_"))));
	            insert.append(QString("'%1'").arg(G_Quote(qbase.value("_DIR_6_"))));
							insert.append(QString("'/lodging/%1/%2/index.html'").arg(la).arg(id));
	            insert.append(QString("'/data/1000/%1/'").arg(id));
	            insert.append(QString("'%1'").arg(qbase.value("_REG_")));

	            QString Dvalue = insert.join(",");
	            QString replace = QString("REPLACE INTO HOUSEPAGE VALUES (%1)").arg(Dvalue);
							QByteArray zzdd = replace.toLatin1();
	return zzdd.toBase64();
	
}

/*

CREATE TABLE `HOUSEPAGE` (
  `xid` varchar(15) NOT NULL default '',
  `xnr` int(32) NOT NULL default '0',
  `xtipo` int(2) NOT NULL default '0',
  `xidcity` int(3) NOT NULL default '0',
  `xtitle` text,
  `xpaese` text,
  `xquery` longtext,
  `xlanguage` char(2) NOT NULL default 'er',
  `xtel_7` varchar(60) default NULL,
  `xtel_8` varchar(60) default NULL,
  `xfax_9` varchar(60) default NULL,
  `xmail_10` varchar(100) default NULL,
  `xnatel_12` varchar(60) default NULL,
  `xwww_11` varchar(100) default NULL,
  `xdir` varchar(255) default NULL,
  `xuri` varchar(255) NOT NULL default '/',
  `xsource` varchar(255) default NULL,
  UNIQUE KEY `xid` (`xid`),
  KEY `xuri` (`xuri`),
  KEY `xsource` (`xsource`),
  KEY `xlanguage` (`xlanguage`),
  FULLTEXT KEY `xquery` (`xquery`)
) ENGINE=MyISAM DEFAULT CHARSET=latin1;

*/


QString HtmlGenerator::HandlerImagesWeb()
{
   QString html;
	 QString firstrela;
	
  if (lipic.size() > 1 ) {
				html.append("<div id=\"PicPosiHouses\"><blockquote class=\"Alps\"><p><b>Foto:</b>    ");
				  for (int i = 0; i < lipic.size(); ++i) {
							SPics conni = lipic[i];
							QString imref = QString("/data/1000/%1/album/%1_%2.jpg").arg(id).arg(i);
						  if (i == 0) {
								firstrela = imref;
							}
						  QString NRT;
							NRT.setNum(i+1);
							html.append(" <a href=\""+imref+"\" rel=\"Houses\" class=\"thickbox\" title=\"Image from "+qbase.value("_NOME_2_")+"\">"+NRT+"</a> |");
					}
				  html.append("</p></blockquote>");
				  html.append("<p><a class=\"thickbox\" rel=\"Houses\" title=\"Image from "+qbase.value("_NOME_2_")+"\" href=\""+firstrela+"\"><img id=\"AlbumScroller\" width=\"246\" title=\"Image from "+qbase.value("_NOME_2_")+"\" src=\""+firstrela+"\" name=\"AlbumScroller\" /></a></p>");
				  html.append("</div>");
				}
			
			
	return html;
	}


QString HtmlGenerator::HandlerWebCode( QString html  )
{
   QString e = html;
	 QString img = HandlerImagesWeb();
	 e = e.replace(xp0,"<p>");
	 e = e.replace(xpx,"<p align=\"center\">");
	 e = e.replace(xpR,"<p align=\"right\">");
	 e = e.replace("./Libero_200.png","/map/theme/magic/image/cal_200.png");
	 e = e.replace("./Prenotato_201.png","/map/theme/magic/image/cal_201.png");
	 e = e.replace("./Provisorio_202.png","/map/theme/magic/image/cal_202.png");
	 e = e.replace("./Chiuso_204.png","/map/theme/magic/image/cal_204.png");
	 e = e.replace("./Riservato_203.png","/map/theme/magic/image/cal_203.png");
	 e = e.replace("./dummyspace.png","/map/theme/magic/image/pix.gif");
	 if (lipic.size() > 1 ) {
	 e = e.replace(TAGIMAGEONE,img);
	 } else {
	 e = e.replace(ImageUrlFirst,"/map/theme/magic/image/pix.gif");
	 }
   return cleaner->TidyCleanhtml(e);
}


QString HtmlGenerator::TitoloGrande()
{
	
	QString testo = QString("<h2>%1").arg(qbase.value("_NOME_2_"));
	int stelle = qbase.value("_STAR_3_").toInt();
	if (stelle > 0) {
	  for (int i = 0; i < stelle; ++i)   {
		testo.append("<img width=\"18\" height=\"18\" class=\"Star\" src=\"/map/theme/magic/image/star.png\" />");
	  }
	
  }
	testo.append("</h2>");
	return testo;
}
















QString HtmlGenerator::calendario( bool web )
{
	drawcalendar = false;
	QString Tabella_Calendario_documento;
	QString calendarurl = QString("/data/1000/%1/calendar.png").arg(id);
	QString annosiamo;       
  annosiamo.setNum(anno_adess0);
	
	
  int playcal = qbase.value("_SHOW_CAL_").toInt();
	if (QCaliVuoto.memo_size() > 1 && playcal > 0) {
		drawcalendar = true;
		 if (!web) {
		     Tabella_Calendario_documento = QString("<h3>" + qtrans.value("Calendario_5") + "</h3>");
	         Tabella_Calendario_documento.append("<br/><img src=\"./nullcalendar.png\" /><br/>");
	         Tabella_Calendario_documento.append(legenda_calendario());
		 } else {
			 //////////PPnuller =  QCaliVuoto.image_year( anno_adess0 );  /* web 20 kb ! */
			 Tabella_Calendario_documento = QString("<h3 class=\"cali\">" + qtrans.value("Calendario_5") + "</h3>");
			 /* image calendar web */
			 Tabella_Calendario_documento += "<a href=\""+calendarurl+"\" title=\"One-Year Calendar "+annosiamo+"\" class=\"thickbox\" ><img title=\"One-Year Calendar "+annosiamo+"\" id=\"WebCalendar\" src=\"/map/theme/magic/image/calendars.jpg\" /></a>";
			 Tabella_Calendario_documento.append("<br/>");
	     Tabella_Calendario_documento.append(legenda_calendario());
		 }
	} else {
		   if (!web) {
		   Tabella_Calendario_documento = "<br/><img src=\"./nullcalendar.png\" /><br/>";
			 }
	}
	
	 ////////QUrl Curlnull(QString("./nullcalendar.png"));
	 ///////////append_pic("./nullcalendar.png",PPnuller);
   return Tabella_Calendario_documento;
}



/*

void  HtmlGenerator::fillImage( QTextDocument * doc ) 
{
    QMapIterator<QString,SPics> e(Fimg);
			while (e.hasNext()) {
				e.next();
				SPics dd = e.value();
			doc->addResource( QTextDocument::ImageResource,QUrl(e.key()),dd.pix());
		}
																				 
}
*/


QString HtmlGenerator::text()
{
	QString testo;
	   if (qbase.size() < 1) {
			 testo = QString("_NO_VALUE_FILLED_");
		 }
		 if (current_lang_pos == 0) {
			 testo =  qbase.value("_TXTIT_101_");
		 } else if (current_lang_pos == 1) {
			 testo =  qbase.value("_TXTDE_102_");
		 } else if (current_lang_pos == 2) {
			 testo =  qbase.value("_TXTFR_103_");
		 } else if (current_lang_pos == 3) {
			 testo =  qbase.value("_TXTEN_104_");
		 } else {
			 testo =  qbase.value("_TXTIT_101_");
		 }
		 return testo;
}



QString HtmlGenerator::legenda_calendario()
{
	DayCell master;
	QString legenda = "<table border=\"0\" style=\" margin-top:0px; margin-bottom:0px; margin-left:0px; margin-right:0px;\" align=\"left\"  cellspacing=\"1\" cellpadding=\"2\" bgcolor=\"#ffffff\"><tr>";
	legenda.append(" <td>"+qtrans.value("Libero_200") + " <img width=\"14\" height=\"14\" src=\"./Libero_200.png\" title=\""+qtrans.value("Libero_200")+"\" />  </td>");
	//////////////legenda.append(" <td>"+qtrans.value("Prenotato_201") + " <img width=\"14\" height=\"14\" src=\"./Prenotato_201.png\" title=\""+qtrans.value("Prenotato_201")+"\" />  </td>");
	//////////////legenda.append(" <td>"+qtrans.value("Provisorio_202") + " <img width=\"14\" height=\"14\" src=\"./Provisorio_202.png\" title=\""+qtrans.value("Provisorio_202")+"\"  />  </td>");
	legenda.append(" <td>"+qtrans.value("Riservato_203") + " <img width=\"14\" height=\"14\" src=\"./Riservato_203.png\" title=\""+qtrans.value("Riservato_203")+"\"  />  </td>");
	legenda.append(" <td>"+qtrans.value("Chiuso_204") + " <img width=\"14\" height=\"14\" src=\"./Chiuso_204.png\" title=\""+qtrans.value("Chiuso_204")+"\" /> </td> ");
  
	/////doc->addResource( QTextDocument::ImageResource,QUrl("./Riservato_203.png"),createColorTool( master.BG(3) ));
	
	///////append_pic("./Libero_200.png",createColorTool( master.BG(0)));
  //////append_pic("./Prenotato_201.png",createColorTool( master.BG(1)));
  /////append_pic("./Provisorio_202.png",createColorTool( master.BG(2)));
  /////////append_pic("./Chiuso_204.png",createColorTool( master.BG(10)));
	//////append_pic("./Riservato_203.png",createColorTool( master.BG(3)));
	
	
	legenda.append("</tr></table>");
	return "<p>" + legenda + "</p>";
}


QString HtmlGenerator::letti()
{
	   QString testo="";
	   if (qtrans.size() < 1) {
			 testo = QString("_NO_VALUE_TR_FILLED_");
		 }
		 //////if (qbase.value("_CAME_30_").toInt() > 0) {
		 testo.append(xp0 + qtrans.value("Camere_22") + " " +  qbase.value("_CAME_30_")   +xp1); 
		 /////////}
		 if (qbase.value("_LETTI_31_").toInt() > 0) {
			 testo.append(xp0 + qtrans.value("Letti_23") + " " +  qbase.value("_LETTI_31_")   +xp1);  
		 }
		 return testo;
}




QString HtmlGenerator::addo()
{
	QString testo="";
	   if (qtrans.size() < 1) {
			 testo = QString("_NO_VALUE_TR_FILLED_");
		 }
		 	//////ID,CID,TIPO_1,TID,NOME_2,VIA_14,STAR_3,CID_4,DIR_6,TEL_7,TEL_8, MIN_P,MAX_P,MIN_C,MAX_C
	//////FAX_9,MAIL_10,WWW_11,CLOSE_40,CLOSE_41,NATEL_12,COORD_15,CAME_30,
	//////LETTI_31,TAR_35,ATT_50,ATT_60,ATT_70,TXT_100,CALWEB_90,NOTE_95,RISE_44,
	/////AUTOR_43,RISE_45,RISE_46,TXTIT_101,TXTDE_102,TXTFR_103,TXTEN_104 FROM %1 WHERE ID = %2 LIMIT 1 ").arg(TABELLA_CASE).arg(db_id),db->current());
		 if ( Tipo_Oggetto == 1 ) {
			testo.append(xp0 + qtrans.value("Proprietario_6")   +xp1);
       testo.append(xp0 +  qbase.value("_DIR_6_")   +xp1);			 
		 } else if ( Tipo_Oggetto == 10 ) {
			testo.append(xp0 + qtrans.value("Gerente_25")  +xp1);
			 testo.append(xp0 +  qbase.value("_DIR_6_")   +xp1);
		 } else {
			testo.append(xp0 + qtrans.value("Direzione_4")  +xp1);
			testo.append(xp0 +  qbase.value("_DIR_6_")   +xp1);
		 }
		 if (qbase.value("_VIA_14_") !="-") {
			testo.append(xp0 +  qbase.value("_VIA_14_")   +xp1);
		 }
		 testo.append(xp0 +  CPAPaese( qbase.value("_CID_4_") )   +xp1);
		 testo.append(xp0 + qtrans.value("Tel_2") + " " +  qbase.value("_TEL_7_")   +xp1);
		 
		 if (qbase.value("_TEL_8_").size() > 5) {
			 testo.append(xp0 + qtrans.value("Tel_2") + " " +  qbase.value("_TEL_8_")   +xp1);
		 }
		 
		 
		 if (qbase.value("_FAX_9_").size() > 5) {
		 testo.append(xp0 + qtrans.value("Fax_3") + " " +  qbase.value("_FAX_9_")   +xp1);
		 }
		 if (qbase.value("_NATEL_12_").size() > 5) {
		 testo.append(xp0 + qtrans.value("Cell_31") + " " +  qbase.value("_NATEL_12_")   +xp1);
		 }
		 testo.append("<br/><br/>");
		 if (qbase.value("_MAIL_10_").size() > 4) {
		 testo.append(xp0 + "E-mail: " +  "<a target=\"_blank\" class=\"Link_External\" href=\"mailto:"+qbase.value("_MAIL_10_")+"\">" + qbase.value("_MAIL_10_") + "<a/>"   + xp1);
		 }
		 if (qbase.value("_WWW_11_").size() > 4) {
		 testo.append(xp0 + "Web: " + "<a target=\"_blank\" class=\"Link_External\" href=\"http://"+qbase.value("_WWW_11_")+"\">" + qbase.value("_WWW_11_") + "<a/>"   +xp1);
		 }
		 testo.append("<br/><br/>");
		 
		 if (qbase.value("_MIN_C_").toDouble() > 1 ) {
		 double mi_cam = qbase.value("_MIN_C_").toDouble();
		 double ma_cam = qbase.value("_MAX_C_").toDouble();
			  /* caso hotel doppia singola */
			testo.append(xp0 + qtrans.value("Al_Giorno_Camera_Prezzo_40") + " "+ QString("CHF %1 / %2 ").arg(mi_cam, 0, 'f', 2).arg(ma_cam, 0, 'f', 2) + " " + QString("*%3 %1.- / %2.- ").arg(mi_cam * FattorePerFranco, 0, 'f', 0).arg(ma_cam * FattorePerFranco, 0, 'f', 0).arg(QChar( 0x20ac ))  +xp1);
		 
		 }
		 if (qbase.value("_MIN_P_").toDouble() > 1 ) {
		 double mi_cam = qbase.value("_MIN_P_").toDouble();
		 double ma_cam = qbase.value("_MAX_P_").toDouble();
		 testo.append(xp0 + qtrans.value("Per_Persona_Giorno_41") + " "+ QString("CHF %1 / %2 ").arg(mi_cam, 0, 'f', 2).arg(ma_cam, 0, 'f', 2) + " " + QString("*%3 %1.- / %2.- ").arg(mi_cam * FattorePerFranco, 0, 'f', 0).arg(ma_cam * FattorePerFranco, 0, 'f', 0).arg(QChar( 0x20ac ))  +xp1);
		 }
		 
		 QDateTime timer1( QDateTime::currentDateTime() ); 
		 
		 if (qbase.value("_CLOSE_40_").toInt() > timer1.toTime_t() && qbase.value("_CLOSE_41_").toInt() > timer1.toTime_t() )  {
			 QDateTime chiuso;
			 QDateTime riapre;
			 chiuso.setTime_t( qbase.value("_CLOSE_40_").toUInt() );
			 riapre.setTime_t( qbase.value("_CLOSE_41_").toUInt() );
			testo.append(xp0 +  qtrans.value("Chiuso_dal_al_9" )+ " " +  HData( chiuso ) + " / " + HData( riapre ) +xp1);
		 }

		 //////MIN_P,MAX_P,MIN_C,MAX_CFattorePerFranco
		 
		  
		 return testo;
}




QString HtmlGenerator::tabella_attributi( QString lista )
{
  QString legenda = "<table border=\"0\" cellspacing=\"1\" cellpadding=\"1\" bgcolor=\"#ffffff\"><tr>";
	QStringList iconummers = lista.split("/",QString::SkipEmptyParts);
	for (int i = 0; i < iconummers.size(); ++i)  {
		legenda.append(Html_Ico(iconummers.at(i).toInt()));
  }
	legenda.append("</tr></table>");
	return xp0 + legenda + xp1;
}


QString HtmlGenerator::Html_Ico( const int nrico  )
{
	if (icones.size() < 1) {
		return QString();
	}
                for (int x=0; x<icones.size(); x++) {
			             IcoLine placehere = icones[x];
									 if (placehere.id() == nrico) {
									 const QString imgref = QString("/map/theme/magic/image/ico/att_%1.png").arg(placehere.id());
									   QString tile = placehere.it;
										 if (current_lang_pos == 1) {
											 tile = placehere.de;
										 } else if (current_lang_pos == 2) {
											 tile = placehere.fr;
										 } else if (current_lang_pos == 3) {
											 tile = placehere.en;
										 }
                     /////////append_pic(imgref,placehere.pix());
									   return QString("<td><img width=\"20\" height=\"20\" class=\"HA_icon\"  src=\""+imgref+"\" title=\""+tile+"\" /></td>");
									 }
	                 
		            }
								
  return QString();
}




QString HtmlGenerator::texter( QString value )
{
	QString txt = Qt::escape(DBase64(value));
	txt = txt.replace("\n","<br/>");
	return txt;
}

QString HtmlGenerator::caratteristiche(bool web)
{
	QString html = "<h3>" + qtrans.value("Caratteristiche_Icone_7")   +  "</h3>";
	if (qbase.value("_ATT_50_").size() > 0 && qbase.value("_ATT_50_") !="/") {
	html.append(xp0 + qtrans.value("Posizione_210") + xp1);
	html.append(tabella_attributi(qbase.value("_ATT_50_")));
	}
	if (qbase.value("_ATT_60_").size() > 0 && qbase.value("_ATT_60_") !="/") {
	html.append(xp0 + qtrans.value("Infrastrutture_211") + xp1);
	html.append(tabella_attributi(qbase.value("_ATT_60_")));
	}
	if (qbase.value("_ATT_70_").size() > 0 && qbase.value("_ATT_70_") !="/" ) {
	html.append(xp0 + qtrans.value("Sport_Salute_212") + xp1);
	html.append(tabella_attributi(qbase.value("_ATT_70_")));
	}
	
	
	  if (lipic.size() > 1 && !web) {
				html.append("<br/><p>");
				
				  for (int i = 0; i < lipic.size(); ++i) {
						if (i >= 1) {
							SPics conni = lipic[i];
							QString imref = QString("./%1_%2.png").arg(id).arg(i);
							 html.append("<img src=\""+imref+"\" />");
						}
						
						
					}
				html.append("</p>");
				
			}
	
	
	
	
	
	
	
	return html;
}

/*
void  HtmlGenerator::append_pic ( const QString def , QPixmap pic  )
{
           SPics current;
           current.set_pics(pic);
           current.name = def;
           Fimg.insert(def,current);
}
*/

QString HtmlGenerator::CPAPaese( const QString  paeseid )
{
  QSqlQueryModel *mod = new QSqlQueryModel();
  mod->setQuery(QString("SELECT name,cpa FROM %1 WHERE id ='%2' LIMIT 1 ").arg(TABELLA_PAESI).arg(paeseid),db->current());
      const int summline = mod->rowCount();
	    if (summline == 1) {
	    QSqlRecord r = mod->record(0);
	    return QString(r.value("cpa").toString()+" "+r.value("name").toString());
			} else {
			return QString();
			}
}

void HtmlGenerator::LoadTrans( const int position )
{
  qtrans.clear();
  current_lang_pos = position;
   QMapIterator<QString,QStringList> i(transall);
       while (i.hasNext()) {
           i.next();
            QStringList alls = i.value();
				 
				    ///////////qDebug() << "### alls " << alls;
            qtrans.insert(i.key(),Qt::escape(alls.at(position)));
       } 
			 
	///////////////qDebug() << "### qtrans count  " << qtrans.size();
  
}


